package com.garvin;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 加班工资计算
 *
 * @author : garvin
 * @version V1.0
 * @date : 2020年11月11日 09:11
 */
public class SalaryForWorkOvertime {
    /**
     * 基本工资
     */
    private static BigDecimal BASE_SALARY = new BigDecimal("8946");
    private static BigDecimal WORK_DAY = new BigDecimal("21.75");
    private static BigDecimal WORK_HOUR = new BigDecimal("8");
    private static BigDecimal hourSalary;

    static {
        hourSalary = BASE_SALARY
                .divide(WORK_DAY, 2, RoundingMode.HALF_UP)
                .divide(WORK_HOUR, 2, RoundingMode.HALF_UP);
    }

    public static void main(String[] args) {

        System.out.println("工作时长：" + workTime("2021-01-27T18:21:00", "2021-01-27T22:05:00", true));
        // 法定节假日 3 倍工资
        BigDecimal salary1 = salary(Multiple.THREE, "0");
        // 周末 2 倍工资
        BigDecimal salary2 = salary(Multiple.TWO, "23.8");
        // 工作日 1.5 倍工资
        BigDecimal salary3 = salary(Multiple.ONE_FIVE, "0");

        System.out.println();
        System.out.println("加班工资合计：" + salary1.add(salary2).add(salary3));
    }

    /**
     * 计算加班工资
     *
     * @param mul  工资倍数
     * @param hour 加班时间（小时）
     */
    private static BigDecimal salary(Multiple mul, String hour) {
        BigDecimal v = hourSalary
                .multiply(mul.getM())
                .multiply(new BigDecimal(hour))
                .setScale(2, RoundingMode.HALF_UP);
        System.out.println(mul.getMsg() + "加班：" + hour + "小时，[" + mul.getM() + "]倍工资：" + v);
        return v;
    }

    enum Multiple {
        ONE_FIVE(new BigDecimal("1.5"), "工作日"),
        TWO(new BigDecimal("2"), "周末"),
        THREE(new BigDecimal("3"), "法定节假日");

        private BigDecimal m;
        private String msg;

        public BigDecimal getM() {
            return m;
        }

        public String getMsg() {
            return msg;
        }

        Multiple(BigDecimal m, String msg) {
            this.m = m;
            this.msg = msg;
        }
    }

    private static String workTime(String begin, String end, boolean eat) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime dateTimeBegin = LocalDateTime.parse(begin, dateTimeFormatter);
        LocalDateTime dateTimeEnd = LocalDateTime.parse(end, dateTimeFormatter);

        Duration duration = Duration.between(dateTimeBegin, dateTimeEnd);
        long minutes = duration.toMinutes();
        BigDecimal divide = new BigDecimal(String.valueOf(minutes)).divide(new BigDecimal("60"), 2, RoundingMode.HALF_UP);
        if (eat) {
            divide = divide.subtract(new BigDecimal("0.5"));
        }
        return divide.toString();
    }
}
